package com.ruoyi.vplat.websocket.listener.edge;

import java.util.HashMap;

import javax.websocket.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.event.NetworkEvent;
import com.ruoyi.common.websocket.handler.AbstractWebsocketMsgHandler;
import com.ruoyi.vplat.websocket.listener.AbstractWsOpenListener;

@Component
public class EdgeWsOpenListener extends AbstractWsOpenListener {
	@Autowired
	private ApplicationEventPublisher eventPublisher;

	@Override
	public void onOpen(String username, Session session) throws Exception {
		//云边协同客户端也是网关
		username = saveOrUpdateGateway(username, session, "云边协同","10000");

		String terminateId = username.toString();
		JSONObject res = AbstractWebsocketMsgHandler.getCommandTemplate("90002", new HashMap<String, Object>() {
			{
				put("protocol", 10000);
				put("seqNum", AbstractWebsocketMsgHandler.getRandomNum(9));
				put("terminateId", terminateId);
			}
		});
		// 统一交给ruoyi-network-server模块处理,方便代码维护
		eventPublisher.publishEvent(new NetworkEvent(this, res.toString()));

	}

	
}
